package droid.frame.utils.lang;

/**
 * 根据字符串不同进行替换
 */
public class StringReplaceUtil {

    /**
     * 根据用户名的不同长度，来进行替换 ，达到保密效果
     *
     * @param userName 用户名
     * @return 替换后的用户名
     */
    public static String userNameReplaceWithStar(String userName) {
        String userNameAfterReplaced = "";

        if (userName == null) {
            userName = "";
        }

        int nameLength = userName.length();

        if (nameLength <= 1) {
            userNameAfterReplaced = "*";
        } else if (nameLength == 2) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{0})\\d(?=\\d{1})", "*");
        } else if (nameLength >= 3 && nameLength <= 6) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{1})\\d(?=\\d{1})", "*");
        } else if (nameLength == 7) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{1})\\d(?=\\d{2})", "*");
        } else if (nameLength == 8) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{2})\\d(?=\\d{2})", "*");
        } else if (nameLength == 9) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{2})\\d(?=\\d{3})", "*");
        } else if (nameLength == 10) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{3})", "*");
        } else if (nameLength >= 11) {
            userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{4})", "*");
        }
        return userNameAfterReplaced;

    }

    /**
     * 实际替换动作
     *
     * @param username username
     * @param regular  正则
     * @return
     */
    private static String replaceAction(String username, String regular, String replacement) {
        return username.replaceAll(regular, replacement);
    }

    /**
     * 身份证号替换，保留前六位和后四位
     * <p>
     * 如果身份证号为空 或者 null ,返回null ；否则，返回替换后的字符串；
     *
     * @param idCard      身份证号
     * @param start       保留前面位数
     * @param end         保留后面的位数
     * @param replacement 要替换成的字符串
     * @return
     */
    public static String idCardReplaceWithStar(String idCard, int start, int end, String replacement) {

        if (idCard.isEmpty() || idCard == null) {
            return null;
        } else {
            String regular = "(?<=\\d{start})\\d(?=\\d{end})";
            return replaceAction(idCard, regular.replace("start", start + "").replace("end", end + ""), replacement);
        }
    }

    /**
     * 银行卡替换，保留后四位
     * <p>
     * 如果银行卡号为空 或者 null ,返回null ；否则，返回替换后的字符串；
     *
     * @param bankCard    银行卡号
     * @param start       保留前面位数
     * @param end         保留后面的位数
     * @param replacement 要替换成的字符串
     * @return
     */
    public static String bankCardReplaceWithStar(String bankCard, int start, int end, String replacement) {

        if (bankCard.isEmpty() || bankCard == null) {
            return null;
        } else {
            String regular = "(?<=\\d{start})\\d(?=\\d{end})";
            return replaceAction(bankCard, regular.replace("start", start + "").replace("end", end + ""), replacement);
        }
    }

}